package com.seaboxdata.controller;

import com.seaboxdata.entity.ElectronicCoupletInfo;
import com.seaboxdata.entity.ElectronicCoupletInfoVo;
import com.seaboxdata.service.ElectronicCoupletInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @create 2021-04-12 14:40
 **/
@RestController
@EnableAutoConfiguration
@RequestMapping("/api/ElectronicCouplet")
@Slf4j
public class ElectronicCoupletController {

    @Value("${default.md5.key}")
    private String key;

    @Autowired
    private ElectronicCoupletInfoService electronicCoupletInfoService;

    @RequestMapping("/endu")
    public Map<String, Object> electronicCoupletInfo(@RequestBody ElectronicCoupletInfoVo electronicCoupletInfoVo) {
        log.info(electronicCoupletInfoVo.toString());
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", "00000");
        resultMap.put("message", "成功");
        resultMap.put("content",null);
        //校验下
        String md5Info = electronicCoupletInfoVo.getMd5Info();
        ElectronicCoupletInfo baseInfo = electronicCoupletInfoVo.getBaseInfo();
        baseInfo.setCjsj(new Date());
        //增加MD5校验,recordId + vehicleNo+ liftTime+约定key
        String md5Key =
                MD5Encoder.encode((baseInfo.getRecordId() + baseInfo.getVehicleNo() + baseInfo.getLiftTime() + key).getBytes());
        if(StringUtils.isEmpty(md5Info) || !md5Info.equals(md5Key)){
            resultMap.put("code", "00004");
            resultMap.put("message", "md5Info为空或md5校验失败");
        }
        try{
            electronicCoupletInfoService.save(baseInfo);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("code", "00001");
            resultMap.put("message", "入库失败");
        }
        return resultMap;
    }

}
